<?php 
require_once '../include/includefiles.php';
require_once '../include/userislogin.php';
?>
<?php 
  if($_POST['title']){
     if(($_FILES["file"]["type"] == "text/html")&&($_FILES["file"]["size"])<20000){
	     if (file_exists("../templates/" . $_FILES["file"]["name"]))
	      {
	         echo alertmsg("文件已经存在");
	      }else{
	      	 move_uploaded_file($_FILES["file"]["tmp_name"],
      "../templates/" . $_FILES["file"]["name"]);
	      }
     }else{
     	alertmsg("文件格式不正确或者文件大小超过限制");
     }
     $title = $_POST['title'];
     $php = $_POST['php'];
     $html = $_FILES["file"]["name"]; 
     if(file_exists("../".$php)){
     	alertmsg("php文件已经存在");
     }

     $query = "select phpdef from siteconfig where id = 1";
     $result = mysql_query($query) or die("获取php默认内容失败");
     $row = mysql_fetch_array($result);
     $phpdef = $row['phpdef'];
     $phpdef = str_replace("xxx",$html,$phpdef);
     $fp=fopen("../".$php,"w+");
     fputs($fp,$phpdef);
     
     $query = " insert into pages(title,php,html,uid)values(";
     $query.= " '".$title."',";
     $query.= " '".$php."',";
     $query.= " '".$html."',";
     $query.= " ".$_SESSION['uid'].")";
     
     $result = mysql_query($query) or die("新增页面失败");
     if($result){
     	alertmsgrtn("新增页面成功","sitepage.php");
     }
  }
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>新增页面</title>
<link href="images/style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="images/jquery-1.7.2.min.js"></script>
<script>
  $(function(){
     $('#title').focus();
     
  });
  function chkfrm(){
    if($('#title').val()==''){
         alert('请输入标题');
         $('#title').focus();
         return false;
    }
    if($('#php').val()==''){
        alert('请输入php文件名称');
        $('#php').focus();
        return false;
    }
    if($('#file').val()==''){
        alert('请上传模板文件');
        return false;
    }
  }
</script>
</head>

<body>
<form name="form1" method="post" action="" enctype="multipart/form-data" onsubmit="return chkfrm();">
  <table width="100%">
    <tr bgcolor="#E1E5EE">
      <td height="30" colspan="2" align="center"><strong>新增页面</strong></td>
    </tr>
    <tr bgcolor="#F7F8F9">
      <td height="30" align="right">名称：</td>
      <td align="left"><input name="title" type="text" id="title"></td>
    </tr>
    <tr bgcolor="#F7F8F9">
      <td width="43%" height="30" align="right">php页面：</td>
      <td width="57%" align="left"><input name="php" type="text" id="php"></td>
    </tr>
    <tr>
      <td height="30" align="right" bgcolor="#F7F8F9">模板页面：</td>
      <td align="left" bgcolor="#F7F8F9"><input id='file' type="file" name="file">
        (后缀为html)</td>
    </tr>
    <tr>
      <td height="30" colspan="2" align="center" bgcolor="#F7F8F9"><input type="submit" name="Submit" value="确认">
          <input type="reset" name="Submit2" value="重置"></td>
    </tr>
  </table>
</form>
</body>
</html>